from django.db import models
from django.contrib.auth.models import AbstractUser


# Create your models here.

class UserInfo(AbstractUser):
    id = models.AutoField(primary_key=True)
    phone = models.CharField(max_length=14, null=True)
    avatar = models.FileField(upload_to="upload/", default="static/img/default.png")
    blog = models.OneToOneField(to="Blog", to_field="id", on_delete=models.CASCADE, null=True)

    class Meta:
        verbose_name = "用户表"
        verbose_name_plural = verbose_name


# 分类表
class Category(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=64, unique=True)
    blog = models.ForeignKey(to="Blog", to_field="id", on_delete=models.CASCADE)

    class Meta:
        verbose_name = "分类"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 标签表
class Tag(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=64)
    blog = models.ForeignKey(to="Blog", to_field="id", on_delete=models.CASCADE)

    class Meta:
        verbose_name = "标签"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


#
class Blog(models.Model):
    """
        博客表， 用户跟博客需要一对一关连
        博客 分类跟标签 做一对多
        一对多特点： 写在多的一方
    """
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=64, verbose_name="标题")
    site_name = models.CharField(max_length=128)
    theme = models.CharField(max_length=128)

    class Meta:
        verbose_name = "博客表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "站点名: {} \t标题: {}".format(self.site_name, self.title)


class Article(models.Model):
    """
        文章表, 一个博客下可以有N个文章
    """
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=256)
    content = models.TextField()
    create_time = models.DateTimeField(auto_now_add=True)
    blog = models.ForeignKey(to='Blog', to_field="id", on_delete=models.CASCADE)
    category = models.ForeignKey(to="Category", to_field="id", on_delete=models.CASCADE)

    commit_num = models.IntegerField(default=0)
    up_num = models.IntegerField(default=0)
    down_num = models.IntegerField(default=0)

    # 第三张表的手动关连， 通过ArticleToTag查找Article是通过哪个字段找，第一个字段写在前
    tag = models.ManyToManyField(to="Tag", through="ArticleToTag", through_fields=("article", "tag"))

    # tag = models.ManyToManyField(to="Tag")


    def __str__(self):
        return "{}--->{}".format(self.title, self.blog.title)


class ArticleToTag(models.Model):
    # 分类与标签做关连
    id = models.AutoField(primary_key=True)
    article = models.ForeignKey(to="Article", to_field="id", on_delete=models.CASCADE)
    tag = models.ForeignKey(to="Tag", to_field="id", on_delete=models.CASCADE)

    def __str__(self):
        return "{}".format(self.article)


class Commit(models.Model):
    """
        评论表,  当@别人之后 获取id号与自己做一个关连
        文章可以对应多个分类，标签
    """
    id = models.AutoField(primary_key=True)
    content = models.CharField(max_length=256)
    create_time = models.DateTimeField(auto_now_add=True)
    article = models.ForeignKey(to="Article", to_field="id", on_delete=models.CASCADE)
    user_id = models.ForeignKey(to="UserInfo", to_field="id", on_delete=models.CASCADE)
    # 可以直接写数字， 查询多次
    # parent_id = models.IntegerField()
    # 通过外建关连查询, 自查
    # parent_id = models.ForeignKey(to="Commit", to_field="id", on_delete=models.CASCADE)
    parent = models.ForeignKey(to="self", to_field="id", on_delete=models.CASCADE, null=True)


# 点赞点踩表
class Upanddown(models.Model):
    id = models.AutoField(primary_key=True)
    article = models.ForeignKey(to="Article", to_field="id", on_delete=models.CASCADE)
    user_id = models.ForeignKey(to="UserInfo", to_field="id", on_delete=models.CASCADE)
    is_check = models.BooleanField()

    class Meta:
        unique_together = (("user_id", "is_check"),)
